import logging
import math
from typing import Optional, List

from fastapi import APIRouter, Depends, Query, Path
from pydantic import Field, BaseModel
from sqlalchemy import text
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from commons.BaseResponse import BaseResponse
from commons.PaginationBase import PaginationBase
from datebase import get_db
from utils.EncryptionUtil import oauth2_scheme, EncryptionMiddleware

my_air_router = APIRouter(prefix="/air", tags=["空气数据"], dependencies=[Depends(oauth2_scheme)])


class ClassroomAirDataSearch(BaseModel):
    """教室空气数据查询请求参数"""
    fpointNo: str = Field("", description="监测点编号")


@my_air_router.get("/getAirDataByFpointNo3", summary="教室空气数据")
def list_air_data_by_fpoint_no3(search: ClassroomAirDataSearch = Query(), db: Session = Depends(get_db)):
    sql = "select fwd,fsd,fmoniter_Pm25,fmoniter_Pm10,fmoniter_Co2,fmoniter_Jq,fmoniter_Yjw"
    sql = sql + " ,faqi,fupddate ftime,fmacno"
    sql = sql + " from a_tc_point_moniter "
    sql = sql + " where fpoint_no= :fpointNo "
    sql = sql + " order by fupddate"
    sql = sql + " limit 1"
    data = db.execute(text(sql), {
        "fpointNo": search.fpointNo,
    }).fetchall()

    d1 = {}
    for key in range(len(data)):
        d1['fpointName'] = None
        d1['fpointNo'] = None
        d1['fwd'] = data[key][0]
        d1['fsd'] = data[key][1]
        d1['fmoniterPm25'] = data[key][2]
        d1['fmoniterPm10'] = data[key][3]
        d1['fmoniterCo2'] = data[key][4]
        d1['fmoniterJq'] = data[key][5]
        d1['fmoniterYjw'] = data[key][6]
        d1['no2'] = None
        d1['so2'] = None
        d1['co'] = None
        d1['o3'] = None
        d1['pm1'] = None
        d1['faqi'] = data[key][7]
        d1['fpointH'] = 0
        d1['fpointW'] = 0
        d1['fpointL'] = 0
        d1['fpointD'] = 0
        d1['ftime'] = data[key][8]
        d1['fequipmentbarcode'] = None
        d1['fmacno'] = data[key][9]
        d1['fequipmenttag'] = None
        d1['fnewwork'] = None
        d1['time'] = 0

    return BaseResponse.success(data=d1)


class ClassroomAirDataSearch2(BaseModel):
    """教室空气数据查询请求参数"""
    fsiteNo: str = Field("", description="监测点编号")


@my_air_router.get("/getAirDataByFsiteNo2", summary="教室空气数据")
def list_air_data_by_fpoint_no2(search: ClassroomAirDataSearch2 = Query(), db: Session = Depends(get_db)):
    sql = " select fpoint_Name,b.fpoint_no,a.fwd,a.fsd,a.fmoniter_pm25"
    sql = sql + " ,a.fmoniter_pm10,a.fmoniter_co2,a.fmoniter_jq,a.fmoniter_yjw,a.fmoniter_no2"
    sql = sql + " ,a.fmoniter_so2,a.fmoniter_co,a.fmoniter_o3,a.faqi,b.fpoint_H"
    sql = sql + " ,b.fpoint_W,b.fpoint_L,b.fpoint_D,a.fpoint_refresh ,c.fequipmentbarcode"
    sql = sql + " ,c.fmacno,c.fequipmenttag"
    sql = sql + " from a_th_point_moniter a,a_ts_point b,s_tc_equipment_inv c"
    sql = sql + " where 1=1"
    sql = sql + " and b.fsite_no= :fsiteNo "
    sql = sql + " and b.fpoint_no=a.fpoint_no"
    sql = sql + " and b.fpoint_no=c.fpoint_no"
    sql = sql + " and c.feuipmentid=7"
    sql = sql + " order by b.fpoint_no"
    data = db.execute(text(sql), {
        "fsiteNo": search.fsiteNo,
    }).fetchall()

    data_list = []
    for key in range(len(data)):
        d1 = {}
        d1['fpointName'] = data[key][0]
        d1['fpointNo'] = data[key][1]
        d1['fwd'] = data[key][2]
        d1['fsd'] = data[key][3]
        d1['fmoniterPm25'] = data[key][4]
        d1['fmoniterPm10'] = data[key][5]
        d1['fmoniterCo2'] = data[key][6]
        d1['fmoniterJq'] = data[key][7]
        d1['fmoniterYjw'] = data[key][8]
        d1['no2'] = None
        d1['so2'] = None
        d1['co'] = None
        d1['o3'] = None
        d1['pm1'] = None
        d1['faqi'] = data[key][13]
        d1['fpointH'] = 0
        d1['fpointW'] = 0
        d1['fpointL'] = 0
        d1['fpointD'] = 0
        d1['ftime'] = data[key][18]
        d1['fequipmentbarcode'] = data[key][19]
        d1['fmacno'] = data[key][20]
        d1['fequipmenttag'] = data[key][21]
        d1['fnewwork'] = None
        d1['time'] = None
        data_list.append(d1)

    info_dict = {"list": data_list}
    return BaseResponse.success(data=info_dict)
